package com.wj.service.impl;

import com.alibaba.fastjson.JSON;
import com.wj.account.AddressInfo;
import com.wj.common.TableId;
import com.wj.dao.AddressDao;
import com.wj.entity.Address;
import com.wj.filter.AccessContext;
import com.wj.service.IAddressService;
import com.wj.vo.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 *  用户地址相关服务接口实现
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressServiceImpl implements IAddressService {

    private final AddressDao addressDao;

    public AddressServiceImpl(AddressDao addressDao) {
        this.addressDao = addressDao;
    }

    @Override
    public TableId createAddressInfo(AddressInfo addressInfo) {
        LoginUserInfo loginUserInfo = AccessContext.getLoginUserInfo();

        // 将传递的参数装换位实体对象
        List<Address> addresses = addressInfo.getAddressItems().stream()
                .map(a -> Address.to(loginUserInfo.getId(), a)).collect(Collectors.toList());
        // 保存到数据表并返回记录的 id 给调用方
        List<Address> saveRecords = addressDao.saveAll(addresses);
        List<Long> ids = saveRecords.stream().map(a -> a.getId()).collect(Collectors.toList());
        log.info("create address info: [{}], [{}]", loginUserInfo.getId(), JSON.toJSONString(ids));

        return new TableId(ids.stream().map(TableId.Id:: new).collect(Collectors.toList()));
    }

    @Override
    public AddressInfo getCurrentAddressInfo() {

        LoginUserInfo loginUserInfo = AccessContext.getLoginUserInfo();

        List<Address> addresses = addressDao.findAllByUserId(loginUserInfo.getId());
        List<AddressInfo.AddressItem> addressItems = addresses.stream().map(Address::toAddressItem).collect(Collectors.toList());
        return new AddressInfo(loginUserInfo.getId(),addressItems);
    }

    @Override
    public AddressInfo getAddressInfoById(Long id) {
        Address address = addressDao.findById(id).orElse(null);
        if (Objects.isNull(address)){
            throw new RuntimeException("address is not exist");
        }
        return new AddressInfo(
                address.getUserId(),
                Collections.singletonList(address.toAddressItem()));
    }

    @Override
    public AddressInfo getAddressInfoByTableId(TableId tableId) {
        List<Long> ids = tableId.getIds().stream().map(TableId.Id::getId).collect(Collectors.toList());
        log.info("get address info by table id: [{}]",JSON.toJSONString(ids));
        List<Address> addresses = addressDao.findAllById(ids);
        if (CollectionUtils.isEmpty(addresses)){
            return new AddressInfo(-1L,Collections.emptyList());
        }
        List<AddressInfo.AddressItem> addressItems = addresses.stream()
                .map(Address::toAddressItem).collect(Collectors.toList());

        return new AddressInfo(addresses.get(0).getUserId(),addressItems);
    }
}
